// pages/register/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        index: 0,
        picker: ['男', '女'],
        date: '2018-12-25',
        password: '', // 确认密码
        passwordShow: false, 
        userinfo: {
            username: '',
            password: '',
            phone: '',
            name: '',
            sex: '',
            birth: '2018-12-25'
        }
        //#region 
        // checkbox: [{
        //     value: 0,
        //     name: '10元',
        //     checked: false,
        //     hot: false,
        // }, {
        //     value: 1,
        //     name: '20元',
        //     checked: true,
        //     hot: false,
        // }, {
        //     value: 2,
        //     name: '30元',
        //     checked: true,
        //     hot: true,
        // }, {
        //     value: 3,
        //     name: '60元',
        //     checked: false,
        //     hot: true,
        // }, {
        //     value: 4,
        //     name: '80元',
        //     checked: false,
        //     hot: false,
        // }, {
        //     value: 5,
        //     name: '100元',
        //     checked: false,
        //     hot: false,
        // }]
        //#endregion
    },
    clickIcon() {
        this.setData({
            passwordShow: !this.data.passwordShow
        })
    },
    //实现双向绑定
    onChange(e) {
        // console.log(e.detail.value)
        // console.log(e.currentTarget.dataset.prop)
        this.setData({
            [e.currentTarget.dataset.prop]: e.detail.value
        })
    },
    // 注册
    register() {
        this.data.userinfo.sex = this.data.picker[this.data.index]
        // console.log(this.data.userinfo)
        let userinfo = this.data.userinfo
        if (userinfo.username == '' || userinfo.password == '' || this.data.password == '' || userinfo.phone == '' || userinfo.sex == '' || userinfo.name == '' || userinfo.date == '') {
            wx.showToast({
                title: '表单不能为空',
                icon: 'error'
            });
        } else {
            // 检查电话的正则
            const regu=/^1\d{10}$/;
            if (!regu.test(userinfo.phone)) {
                wx.showToast({
                    title: '电话格式错误',
                    icon: 'error'
                });
                return;
            }
            if (userinfo.password === this.data.password) {
                const db = wx.cloud.database()
                const _ = db.conmmand
                var that = this
                // 验证用户是否已经被注册
                db.collection('user').where({
                    username: that.data.userinfo.username
                }).get({
                    success: function (res) {
                        console.log(res.data)
                        if (res.data != '') {
                            wx.showToast({
                                title: '该用户已被注册',
                                icon: 'error'
                            })
                        } else {
                            // 添加用户信息
                            db.collection('user').add({
                                data: {
                                    "username": that.data.userinfo.username,
                                    "password": that.data.userinfo.password,
                                    "phone": that.data.userinfo.phone,
                                    "name": that.data.userinfo.name,
                                    "sex": that.data.userinfo.sex,
                                    "birth": that.data.userinfo.birth,
                                    "faceImg": "cloud://cloud1-3gwgwntc19418eac.636c-cloud1-3gwgwntc19418eac-1312065174/orderimg/2022/06/29 15:00:39-621403.png",
                                    "login": 0,
                                    "eventDone": 0,
                                    "eventSum": 0,
                                    "eventTo": 0,
                                    "score": 0,
                                    "clocks":[],
                                    "sign": 0,
                                    "serviceNum": 0,
                                    "serviceTime": 0,
                                    "exp": 0,
                                }
                            })
                            //返回上一级，关闭当前页面
                            wx.navigateBack({
                                delta: 1
                            })
                            wx.showToast({
                                title: '注册成功',
                                icon: 'success'
                            })
                        }
                    }
                })
            } else {
                wx.showToast({
                    title: '密码不一致',
                    icon: 'error'
                })
            }
        }

    },
    showModal(e) {
        this.setData({
            modalName: e.currentTarget.dataset.target
        })
    },

    hideModal(e) {
        this.setData({
            modalName: null
        })
    },
    ChooseCheckbox(e) {
        let items = this.data.checkbox;
        let values = e.currentTarget.dataset.value;
        for (let i = 0, lenI = items.length; i < lenI; ++i) {
            if (items[i].value == values) {
                items[i].checked = !items[i].checked;
                break
            }
        }
        this.setData({
            checkbox: items
        })
    },
    PickerChange(e) {
        console.log(e);
        this.setData({
            index: e.detail.value
        })
    },
    DateChange(e) {
        this.setData({
            'userinfo.birth': e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})